package cn.gok.oa.attendance.service.impl;

import cn.gok.oa.attendance.entity.TbApply;
import cn.gok.oa.attendance.entity.TbAttendance;
import cn.gok.oa.attendance.mapper.TbApplyMapper;
import cn.gok.oa.attendance.mapper.TbAttendanceMapper;
import cn.gok.oa.attendance.service.ITbApplyService;
import cn.gok.oa.system.entity.TbUser;
import cn.gok.oa.system.mapper.TbUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@Service
public class TbApplyServiceImpl extends ServiceImpl<TbApplyMapper, TbApply> implements ITbApplyService {
    @Autowired
    TbAttendanceMapper attendanceMapper;
    @Autowired
    TbUserMapper userMapper;
    @Autowired
    TbApplyMapper applyMapper;
    @Override
    public int apply(int attendanceId) {
        TbAttendance attendance = attendanceMapper.selectById(attendanceId);
        TbApply apply = new TbApply();
        apply.setUserId(attendance.getUserId());
        apply.setAttendanceId(attendanceId);
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        //根据部门id和角色id定位出部门主管的员工id
        queryWrapper.lambda().eq(TbUser::getDeptId,attendance.getDeptId());
        queryWrapper.lambda().eq(TbUser::getRoleId,2);
        //将部门主管id插入apply，后续查询审批时，根据部门主管id查看审批记录
        apply.setApproverId(userMapper.selectOne(queryWrapper).getId());
        return applyMapper.insert(apply);
    }

    @Override
    public List<TbApply> queryByUserId(int userId) {
        QueryWrapper<TbApply> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(TbApply::getApproverId,userId);
        return applyMapper.selectList(queryWrapper);
    }
}
